package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.HandleEventAdapter;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.EventE;
import com.infore.airsupervise.biz.EventBiz;
import com.infore.airsupervise.biz.impl.EventBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.EmptyView;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 事件派遣列表界面
 * Created by xiaotao on 2017/8/15.
 */

public class HandleEventsListActivity extends BaseActivity
        implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener{


    @BindView(R.id.titlebar_left_menu)
    AwesomeTextView titlebarLeftMenu;
    @BindView(R.id.titlebar_title)
    TextView titlebarTitle;
    @BindView(R.id.titlebar_right_menu)
    AwesomeTextView titlebarRightMenu;

    @BindView(R.id.handle_event_search)
    TextView handleEventSearch;
    @BindView(R.id.handle_event_search_show)
    LinearLayout handleSearchShow;
    @BindView(R.id.refreshrecyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.handle_event_refresh)
    SwipeRefreshLayout mRefresh;

    private EmptyView emptyView; // 无数据时填充view

    private List<EventE> bean = new ArrayList<>();

    private EventBiz eventBiz;
    private HandleEventAdapter mAdapter;

    private int refreshpagemun=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handle_event);
        ButterKnife.bind(this);
        eventBiz=new EventBizImpl();
        initView();
        initViewPager();

    }

    private void initView() {
        titlebarLeftMenu.setFontAwesomeIcon(FontAwesome.FA_CHEVRON_LEFT);
        titlebarRightMenu.setVisibility(View.INVISIBLE);
        titlebarTitle.setText("事件处理");
        mAdapter = new HandleEventAdapter(R.layout.activity_event_item);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                EventE eventE = (EventE) adapter.getItem(position);
                Intent intent = new Intent(ctx, EventDetailsActivity.class);
                intent.putExtra("eventid", eventE.getId());
                intent.putExtra("flag", eventE.getFlag());
                startActivityForResult(intent,400);

            }
        });

        handleSearchShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtil.jumpFromTo(HandleEventsListActivity.this, SearchDataRateActivity.class, Constants.INTENT_INT_KEY , -1);
                overridePendingTransition(R.anim.slide_up_in,
                        R.anim.slide_up_out);
            }
        });
        mRefresh.setOnRefreshListener(this);
        mRefresh.setColorSchemeColors(Color.rgb(47, 223, 189));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRefresh.setRefreshing(true);
        this.onRefresh();

        // 设置emptyView
        emptyView = new EmptyView(ctx);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRefresh.setRefreshing(true);
                onRefresh();
            }
        });
    }

    private void initViewPager() {

    }


    /**
     * 返回上个界面
     */
    @OnClick(R.id.titlebar_left_menu)
    void leftMenuClick() {
        finish();
    }

    /**
     * 获取处理列表
     * @param isRefresh
     */
    private void getHandleEventDatas(final boolean isRefresh) {
        eventBiz.getHandlerEventList(refreshpagemun, new OnServerResponseListener<List<EventE>>() {
            @Override
            public void onSucceed(List<EventE> result, String desc) {

                if (isRefresh) {
                    mAdapter.setNewData(result);
                    mRefresh.setRefreshing(false);
                    mAdapter.setEnableLoadMore(true);
                } else {
                    mAdapter.addData(result);
                    mAdapter.loadMoreComplete();
                    mRefresh.setEnabled(true);
                }

                if (result == null || result.size() == 0) {
                    emptyView.setShowInfo(R.drawable.status_search_null, "暂无数据");
                    mAdapter.setEmptyView(emptyView);
                } else if (result.size() < 20){
                    mAdapter.loadMoreEnd(true);
                }

                refreshpagemun++;
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                if (isRefresh) {

                    int imgRes = R.drawable.status_connect_failed;
                    switch (errorCode) {
                        case StatusConst.CODE_CONNECT_FAIL:
                            imgRes = R.drawable.status_connect_failed;
                            break;

                        case StatusConst.CODE_CONNECT_TIMEOUT:
                            imgRes = R.drawable.status_timeout;
                            break;

                        case StatusConst.CODE_SERVER_EXCEPTION:
                            imgRes = R.drawable.status_server_fix;
                            break;

                        case StatusConst.CODE_OPERATION_FAIL:
                            imgRes = R.drawable.status_connect_failed;
                            break;
                    }

                    emptyView.setShowInfo(imgRes, title);
                    mAdapter.setEmptyView(emptyView);

                    // 结束刷新
                    mRefresh.setRefreshing(false);

                } else {
                    // 结束加载
                    mAdapter.loadMoreFail();
                }
            }
        });
    }


    @Override
    public void onRefresh() {
        refreshpagemun=0;
        mAdapter.setEnableLoadMore(false);

        getHandleEventDatas(true);
    }

    @Override
    public void onLoadMoreRequested() {
        mRefresh.setEnabled(false);

        getHandleEventDatas(false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == 400){
            mRefresh.setRefreshing(true);
            this.onRefresh();
        }
    }
}
